<template>
  <el-row style="width: 100%">
    <el-col :span="20">{{ treeNode.name }}</el-col>
    <el-col class="right" :span="4">
      <el-row>
        <el-col :span="12">{{ treeNode.manager }}</el-col>
        <el-col :span="12">
          <el-dropdown @command="handleCommand">
            <span class="el-dropdown-link">
              操作<i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <el-dropdown-item command="edit" v-if="!isRoot"
                >查看部门</el-dropdown-item
              >

              <el-dropdown-item command="del" v-if="!isRoot"
                >删除部门</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delGepartment, getGepartment } from '@/api/department'
export default {
  props: {
    treeNode: {
      required: true, // 设置当前数据为必填
      type: Object // 类型是Object
    },
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    handleCommand (command) {
      switch (command) {
        case 'add':
          // 新增
          this.$emit('addDept', this.treeNode.id)
          break
        case 'edit':
          // 编辑
          this.$emit('editDept', this.treeNode.id)
          break
        case 'del':
          // 删除
          this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          })
            .then(() => {
              delGepartment(this.treeNode.id),
                getGepartment(),
                this.$emit('delDets'),
                this.$message({
                  type: 'success',
                  message: '删除成功!'
                })
            })
            .catch(() => {
              this.$message({
                type: 'info',
                message: '已取消删除'
              })
            })
          break
      }
    }
  }
}
</script>

<style lang="scss">
.right {
  color: #f09;
  font-size: 14px;
  text-align: right;
}
</style>
